package org.convallaria.system.biz.service;

/**
 * 密码策略服务接口
 * 
 * @author convallaria
 * @since 1.0.0
 */
public interface PasswordPolicyService {

    /**
     * 验证密码策略
     * 
     * @param password 密码
     * @param userId 用户ID（用于历史密码检查）
     * @return 验证结果
     */
    PasswordValidationResult validatePassword(String password, Long userId);

    /**
     * 检查密码是否在历史记录中
     * 
     * @param password 密码
     * @param userId 用户ID
     * @return 是否在历史记录中
     */
    boolean isPasswordInHistory(String password, Long userId);

    /**
     * 检查是否为常见密码
     * 
     * @param password 密码
     * @return 是否为常见密码
     */
    boolean isCommonPassword(String password);

    /**
     * 获取密码策略配置
     * 
     * @return 密码策略配置
     */
    PasswordPolicyConfig getPasswordPolicyConfig();

    /**
     * 密码验证结果
     */
    class PasswordValidationResult {
        private boolean valid;
        private String message;
        private PasswordPolicyViolation violation;

        public PasswordValidationResult(boolean valid, String message, PasswordPolicyViolation violation) {
            this.valid = valid;
            this.message = message;
            this.violation = violation;
        }

        public static PasswordValidationResult success() {
            return new PasswordValidationResult(true, "密码验证通过", null);
        }

        public static PasswordValidationResult failure(String message, PasswordPolicyViolation violation) {
            return new PasswordValidationResult(false, message, violation);
        }

        // Getters
        public boolean isValid() { return valid; }
        public String getMessage() { return message; }
        public PasswordPolicyViolation getViolation() { return violation; }
    }

    /**
     * 密码策略违规类型
     */
    enum PasswordPolicyViolation {
        TOO_SHORT("密码长度不足"),
        TOO_LONG("密码长度过长"),
        MISSING_LOWERCASE("缺少小写字母"),
        MISSING_UPPERCASE("缺少大写字母"),
        MISSING_DIGIT("缺少数字"),
        MISSING_SPECIAL_CHAR("缺少特殊字符"),
        IN_HISTORY("密码在历史记录中"),
        COMMON_PASSWORD("密码过于简单"),
        CONTAINS_USERNAME("密码包含用户名"),
        CONTAINS_PERSONAL_INFO("密码包含个人信息");

        private final String description;

        PasswordPolicyViolation(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 密码策略配置
     */
    class PasswordPolicyConfig {
        private int minLength = 8;
        private int maxLength = 32;
        private boolean requireLowercase = true;
        private boolean requireUppercase = true;
        private boolean requireDigit = true;
        private boolean requireSpecialChar = true;
        private int historyCount = 5;
        private boolean checkCommonPasswords = true;
        private boolean checkUsername = true;
        private boolean checkPersonalInfo = true;

        // Getters and Setters
        public int getMinLength() { return minLength; }
        public void setMinLength(int minLength) { this.minLength = minLength; }
        
        public int getMaxLength() { return maxLength; }
        public void setMaxLength(int maxLength) { this.maxLength = maxLength; }
        
        public boolean isRequireLowercase() { return requireLowercase; }
        public void setRequireLowercase(boolean requireLowercase) { this.requireLowercase = requireLowercase; }
        
        public boolean isRequireUppercase() { return requireUppercase; }
        public void setRequireUppercase(boolean requireUppercase) { this.requireUppercase = requireUppercase; }
        
        public boolean isRequireDigit() { return requireDigit; }
        public void setRequireDigit(boolean requireDigit) { this.requireDigit = requireDigit; }
        
        public boolean isRequireSpecialChar() { return requireSpecialChar; }
        public void setRequireSpecialChar(boolean requireSpecialChar) { this.requireSpecialChar = requireSpecialChar; }
        
        public int getHistoryCount() { return historyCount; }
        public void setHistoryCount(int historyCount) { this.historyCount = historyCount; }
        
        public boolean isCheckCommonPasswords() { return checkCommonPasswords; }
        public void setCheckCommonPasswords(boolean checkCommonPasswords) { this.checkCommonPasswords = checkCommonPasswords; }
        
        public boolean isCheckUsername() { return checkUsername; }
        public void setCheckUsername(boolean checkUsername) { this.checkUsername = checkUsername; }
        
        public boolean isCheckPersonalInfo() { return checkPersonalInfo; }
        public void setCheckPersonalInfo(boolean checkPersonalInfo) { this.checkPersonalInfo = checkPersonalInfo; }
    }
}
